package websocket.listener;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import websocket.enum_.MsgType;
import websocket.socket_.SocketMsg;
import websocket.socket_.WebSocketServer;
import websocket.util.DateUtil;
import websocket.util.DigitalTrans;
import websocket.util.SerialUtil;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName ZtSerialListener
 * @Des TODO 内环转台串口监听
 * @Author lg
 * @Date 2022/7/7 10:54
 * @Version 1.0.0
 **/
public class NhZtSerialListener extends PortListener {

    private static final Logger log = LoggerFactory.getLogger(NhZtSerialListener.class);

    @Override
    public void onReadException(Exception e) {
        System.out.println("发生异常: " + e.getMessage());
    }

    @Override
    public void onReceive(byte[] data) {
        List resultData = new ArrayList<>();
        double x = 0, y = 0, q1x = 0, q1y = 0, q2x = 0, q2y = 0, q1_jiajiao = 0, q2_jiajiao = 0;
        SerialUtil.decode(data, x, y, q1x, q1y, q2x, q2y, q1_jiajiao, q2_jiajiao, resultData);
        System.err.println(resultData);
        try {
            // 判断数据是否为空
            if (CollectionUtils.isEmpty(resultData)) {
                return;
            }
            SocketMsg socketMsg = new SocketMsg(JSONObject.toJSONString(resultData), MsgType.SZ_ZT);
            WebSocketServer.sendInfo(socketMsg, "nrj");
            log.info("Socket消息发送成功,【转台-外环信息】时间:" + DateUtil.formatNow());
        } catch (IOException e) {
            log.error("Socket消息发送失败,【转台-外环信息】时间:" + DateUtil.formatNow());
            e.printStackTrace();
        }
    }
}
